﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace iOak.Web.Modules
{
    public partial class PagerControl : System.Web.UI.UserControl
    {
        public delegate void OnPageIndexChangeHandler(object serder, EventArgs e);
        public event OnPageIndexChangeHandler PageIndexChange;
        protected GridView PagerGrid;
        protected IList DataSource;
        protected int PageSize;
        protected int PageIndex;
        private PageChangeMode? DoPageChangeMode = null;
        public int PageCount
        {
            set { this.ViewState["PageCount"] = value; }
            get
            {
                int pageCount = 0;
                if (this.ViewState["PageCount"] == null)
                {
                    pageCount = int.Parse(this.ViewState["PageCount"].ToString());
                }
                return pageCount;
            }
        }

        public void BindData(GridView gv, IList dataSource, int pageSize = 10)
        {
            gv.AllowPaging = true;
            gv.PageSize = pageSize;
            gv.PagerSettings.Visible = false;
            if (DoPageChangeMode != null)
            {
                switch (DoPageChangeMode.Value)
                {
                    case PageChangeMode.First:
                        PageIndex = 0;
                        break;
                    case PageChangeMode.Previous:
                        PageIndex = gv.PageIndex - 1;
                        break;
                    case PageChangeMode.Next:
                        PageIndex = gv.PageIndex + 1;
                        break;
                    case PageChangeMode.Last:
                        PageIndex = gv.PageCount;
                        break;
                }
                gv.PageIndex = PageIndex;
            }
            PagerGrid = gv;
            DataSource = dataSource;
            PageSize = pageSize;
            PagerGrid.DataSource = DataSource;
            PagerGrid.DataBind();
            SetPageControlsEnabled();
            lblTotalCount.Text = "共" + dataSource.Count + "条记录";
            PageCount = dataSource.Count;
            plNoData.Visible = dataSource.Count == 0;
            btnGoto.Enabled = PagerGrid.PageCount != 0;
        }

        private void SetPageControlsEnabled()
        {
            lbtnFirst.Enabled = PagerGrid.PageIndex != 0;
            lbtnPrevious.Enabled = PagerGrid.PageIndex != 0;
            lbtnLast.Enabled = PagerGrid.PageIndex < PagerGrid.PageCount - 1;
            lbtnNext.Enabled = PagerGrid.PageIndex < PagerGrid.PageCount - 1;
        }

        protected void lbtnPrevious_Click(object sender, EventArgs e)
        {
            PageGoto(PageChangeMode.Previous);
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            PageGoto(PageChangeMode.Next);
        }

        protected void btnGoto_Click(object sender, EventArgs e)
        {
            PageIndex = int.Parse(txtPageIndex.Text) - 1;
            PageGoto(PageChangeMode.Goto);
        }

        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            PageGoto(PageChangeMode.First);
        }

        protected void lbtnLast_Click(object sender, EventArgs e)
        {
            PageGoto(PageChangeMode.Last);
        }

        private void PageGoto(PageChangeMode pageChangeMode)
        {
            if (PageIndexChange != null)
            {
                DoPageChangeMode = pageChangeMode;
                PageIndexChange(this, EventArgs.Empty);
            }
        }
    }

    enum PageChangeMode
    {
        First,
        Previous,
        Next,
        Last,
        Goto
    }
}